﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.ComponentModel;
using System.Collections.ObjectModel;


namespace ScanBook
{
    /// <summary>
    /// Interaction logic for UC_PageChangeReminder.xaml
    /// </summary>
    public partial class UC_PageChangeReminder : UserControl
    {
        public delegate void OnChangePage(bool IsLastPage);
        public OnChangePage ocp = null;
        //
        public int ExtraPage { get; private set; }
        //
        //enum SideNumber { First = 1, Second = 2 }
        //private SideNumber sideNumber = SideNumber.First;
        private int lastPageNumber = 1;

        public UC_PageChangeReminder()
        {
            InitializeComponent();
            //
            text.Background = Brushes.LightGreen;
            //
        }

        private void check_Click(object sender, RoutedEventArgs e)
        {
            if ((bool)check.IsChecked)
            {
                ExtraPage = 1;
            }
            else
            {
                ExtraPage = 0;
            }
            //SetPageNumber(lastPageNumber);
        }

        private int countPageSide(int imageNumber)
        {
            return (0 == ((imageNumber + ExtraPage) % 2)) ? 1 : 2;
        }

        public void SetPageNumber(int pageNumber, bool asStandard)
        {
            lastPageNumber = pageNumber;
            //
            int sideNumber = countPageSide(pageNumber);
            text.Text = string.Format("{0} / 2", sideNumber);
            //
            if (asStandard) showPageNumberAsStd();
            else
            {
                if (2 == sideNumber)
                {
                    showPageNumberAsRed();
                }
                else
                {
                    showPageNumberAsStd();
                }
            }
        }

        public bool IsTimeToChangePage()
        {
            return 2 == countPageSide(lastPageNumber);
        }

#if false
        public void SetPageNumber(int pageNumber)
        {
            lastPageNumber = pageNumber;
            text.Background = Brushes.LightGreen;
            //
            int sideNumber = (0 != ((pageNumber + ExtraPage) % 2)) ? 1 : 2;
            text.Text = string.Format("{0} / 2", sideNumber);
            text.Refresh();
            System.Threading.Thread.Sleep(100);
        }

        public bool MarkNewSheet()
        {
            if (sideNumber == SideNumber.Second)
            {
                text.Background = Brushes.Coral;
                if (null != ocp) ocp(true);
            }
            else
            {
                text.Background = Brushes.LightGreen;
                if (null != ocp) ocp(false);
            }
            text.Refresh();
            System.Threading.Thread.Sleep(100);
            //
            return text.Background == Brushes.Coral;
        }
#endif
        private void showPageNumberAsStd()
        {
            text.Background = Brushes.LightGreen;
            text.Refresh();
            System.Threading.Thread.Sleep(100);
        }
        private void showPageNumberAsRed()
        {
            text.Background = Brushes.Coral;
            text.Refresh();
            System.Threading.Thread.Sleep(100);
        }

    }//class


    /*
    //http://geekswithblogs.net/NewThingsILearned/archive/2008/08/25/refresh--update-wpf-controls.aspx
    public static class ExtensionMethods
    {
        private static Action EmptyDelegate = delegate() { };
        public static void Refresh(this UIElement uiElement)
        {
            uiElement.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Render, EmptyDelegate);
        }
    }
    */

}//namespace
